using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;
namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CGMSdkuData : Class
    {
        #region Declares
        int _cellid = -1;
        int _sdkuid = -1;
        int _row = -1;
        int _col = -1;
        int _number = -1;
        int _readonly = -1;
        #endregion

        #region Constructors
        public CGMSdkuData()
        {
        }
        public CGMSdkuData(CLogin Login)
        {
            base.Login = Login;
        }
        public CGMSdkuData(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBGMSdkuData db = new AccDBGMSdkuData())
            {
                this.Populate((CGMSdkuData)db.GetClass(Login, ObjId));
            }
        }
        public CGMSdkuData(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CGMSdkuData(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBGMSdkuData db = new AccDBGMSdkuData())
            {
                this.Populate((CGMSdkuData)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("cellid") >= 0)
                    {
                        this._cellid = (dr["cellid"] != DBNull.Value ? Convert.ToInt32(dr["cellid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGMSdkuData ADD cellid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("sdkuid") >= 0)
                    {
                        this._sdkuid = (dr["sdkuid"] != DBNull.Value ? Convert.ToInt32(dr["sdkuid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGMSdkuData ADD sdkuid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("row") >= 0)
                    {
                        this._row = (dr["row"] != DBNull.Value ? Convert.ToInt32(dr["row"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGMSdkuData ADD row [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("col") >= 0)
                    {
                        this._col = (dr["col"] != DBNull.Value ? Convert.ToInt32(dr["col"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGMSdkuData ADD col [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("number") >= 0)
                    {
                        this._number = (dr["number"] != DBNull.Value ? Convert.ToInt32(dr["number"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGMSdkuData ADD number [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("readonly") >= 0)
                    {
                        this._readonly = (dr["readonly"] != DBNull.Value ? Convert.ToInt32(dr["readonly"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblGMSdkuData ADD readonly [int] NULL");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CGMSdkuData Obj)
			{
				if (Obj != null)
				{
					try
					{
						this._cellid	 = Obj.cellid;
						this._sdkuid	 = Obj.sdkuid;
						this._row	 = Obj.row;
						this._col	 = Obj.col;
						this._number	 = Obj.number;
						this._readonly	 = Obj.readOnly;
					}
					catch{}
				}
			}
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBGMSdkuData db = new AccDBGMSdkuData())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBGMSdkuData db = new AccDBGMSdkuData())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBGMSdkuData db = new AccDBGMSdkuData())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBGMSdkuData db = new AccDBGMSdkuData())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CGMSdkuData obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CGMSdkuData GMSdkuData);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CGMSdkuData GMSdkuData);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CGMSdkuData GMSdkuData);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CGMSdkuData GMSdkuData);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CGMSdkuData obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int cellid
        {
            get { return this._cellid; }
            set
            {
                if (this._cellid != value)
                {
                    this._cellid = value;
                }
            }
        }
        public int sdkuid
        {
            get { return this._sdkuid; }
            set
            {
                if (this._sdkuid != value)
                {
                    this._sdkuid = value;
                }
            }
        }
        public int row
        {
            get { return this._row; }
            set
            {
                if (this._row != value)
                {
                    this._row = value;
                }
            }
        }
        public int col
        {
            get { return this._col; }
            set
            {
                if (this._col != value)
                {
                    this._col = value;
                }
            }
        }
        public int number
        {
            get { return this._number; }
            set
            {
                if (this._number != value)
                {
                    this._number = value;
                }
            }
        }
        public int readOnly
        {
            get { return this._readonly; }
            set
            {
                if (this._readonly != value)
                {
                    this._readonly = value;
                }
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBGMSdkuData : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBGMSdkuData()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT GMSdkuDataId, GMSdkuDataName";
                sql += " FROM  tblGMSdkuData";
                sql += " WHERE (GMSdkuDataId >= 0)";
                sql += " ORDER BY GMSdkuDataName";
                DataTable table = p.OpenTable(sql, "tblGMSdkuData");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["GMSdkuDataName"] = extName;
                        row["GMSdkuDataId"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "GMSdkuDataName";
                    cmb.ValueMember = "GMSdkuDataId";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tblGMSdkuData WHERE (cellid = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tblGMSdkuData");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CGMSdkuData(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)	
			{
				CGMSdkuData GMSdkuData = (CGMSdkuData)obj;
				string sql ="SELECT * FROM tblGMSdkuData";
                sql += " WHERE cellid =" + GMSdkuData.cellid + "";
				//sql +=" AND CompanyId =" + GMSdkuData.CompanyId + "";
				DataTable dt = p.OpenTable(sql, "tblGMSdkuData");
				if (dt != null)
				{
					DataRow row = null;
                    if (dt.Rows.Count == 1 && GMSdkuData.cellid != -1)
					{
						row = dt.Rows[0];
					}
					else
					{
                        GMSdkuData.cellid = Convert.ToInt32(p.InitIDKey("cellid", "tblGMSdkuData", null));
						row = dt.NewRow();
						dt.Rows.Add(row);
					}
					row["cellid"] = GMSdkuData.cellid;
					row["sdkuid"] = GMSdkuData.sdkuid;
					row["row"] = GMSdkuData.row;
					row["col"] = GMSdkuData.col;
					row["number"] = GMSdkuData.number;
					row["readonly"] = GMSdkuData.readOnly;
					if (!p.UpdateTable("tblGMSdkuData", dt))
					{
						return false;
					}
					return true;
				}
				return false;
			}
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CGMSdkuData GMSdkuData = (CGMSdkuData)obj;
            string sql = "DELETE FROM tblGMSdkuData";
            sql += " WHERE cellid =" + GMSdkuData.cellid + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool Delete(CLogin Login, CGMSdku obj)
        {
              IProvider p = new OleDb(Login.Connectionstring);
              if (p.Open())
              {
                  p.OpenTransaction();
                  string sql = "DELETE FROM tblGMSdkuData";
                  sql += " WHERE sdkuid =" + obj.sdkuid + "";
                  if (p.ExecuteNonQuery(sql))
                  {
                      p.CloseTransaction();
                      p.Close();
                      return true;
                  }
                  p.Close();
              }
              return false;
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblGMSdkuData";
            DataTable table = p.OpenTable(sql, "tblGMSdkuData");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CGMSdkuData o = new CGMSdkuData(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
            string sql = "SELECT * FROM tblGMSdkuData WHERE sdkuid=" + ((CGMSdku)obj).sdkuid;
            DataTable table = p.OpenTable(sql, "tblGMSdkuData");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CGMSdkuData o = new CGMSdkuData(obj.Login, p, row);
                    Collection.Add(o);
                }
            }
        }
    }
    #endregion
}
